package bll

import (
	"jgt-oper/src/model"
	"jgt-oper/src/schema"
	"jgt-oper/src/util"
	"time"
)

// ResourceManagement 公共资源分类管理
type ResourceManagement struct {
	RMModel   model.IResourceManagement     `inject:"IResourceManagement"`
	RCModel   model.IResourceClassification `inject:"IResourceClassification"`
	ParkModel model.IPark                   `inject:"IPark"`
}

// QueryPage 查询分页
func (a *ResourceManagement) QueryPage(param *schema.RMQParams, pageIndex uint, pageSize uint) (int, []*schema.ResourceManagement, error) {
	return a.RMModel.QueryPage(param, pageIndex, pageSize)
}

// Create 创建资源
func (a *ResourceManagement) Create(item *schema.ResourceManagement) error {
	item.ID = 0
	item.RecordID = util.UUID()
	item.Created = time.Now().Unix()
	item.Deleted = 0
	return a.RMModel.Create(item)
}

// Get 查询资源
func (a *ResourceManagement) Get(param string) (*schema.ResourceManagement, error) {
	return a.RMModel.Get(param)
}

// Update 更新资源
func (a *ResourceManagement) Update(id string, item *schema.ResourceManagement) error {
	info := util.StructToMap(item)
	delete(info, "id")
	delete(info, "record_id")
	delete(info, "creator")
	delete(info, "created")
	delete(info, "deleted")

	return a.RMModel.Update(id, info)
}

// Delete 标记删除
func (a *ResourceManagement) Delete(param string) error {
	return a.RMModel.Delete(param)
}

// QueryCategory 查询公共资源分类信息中的前50条信息
func (a *ResourceManagement) QueryCategory() ([]*schema.ResourceClassification, error) {
	_, items, err := a.RCModel.QueryPage("", 1, 50)
	return items, err
}

// QueryParksZones 查询园区建筑节点
func (a *ResourceManagement) QueryParksZones() ([]*schema.Park, error) {
	return a.ParkModel.QueryAllNodes()
}

// QueryAvailableRes 查询可用资源
func (a *ResourceManagement) QueryAvailableRes() ([]*schema.ResourceManagement, error) {
	return a.RMModel.QueryAvailableRes()
}
